# library.mk
#
# Date: July 28, 2016
# Author: Brandon Perez
# Author: Jared Choi
#
# The Makefile for building the AXI DMA library.

# Include guard for the Makefile
ifndef LIBAXIDMA_MAKEFILE_
LIBAXIDMA_MAKEFILE_=included

################################################################################
# Configuration
################################################################################

# The flags for compiling the library
LIBAXIDMA_CFLAGS = $(GLOBAL_CFLAGS) -fPIC -shared \
				   -Wno-missing-field-initializers

# The files that makeup the AXI DMA library
LIBAXIDMA_DIR = library
LIBAXIDMA_FILES = libaxidma.c
LIBAXIDMA = $(addprefix $(LIBAXIDMA_DIR)/,$(LIBAXIDMA_FILES))

# The header files for the AXI DMA library interface
LIBAXIDMA_INC_DIRS = include
LIBAXIDMA_INC_FILES = libaxidma.h axidma_ioctl.h
LIBAXIDMA_INC = $(addprefix $(LIBAXIDMA_INC_DIRS)/,$(LIBAXIDMA_INC_FILES))
LIBAXIDMA_INC_FLAGS = $(addprefix -I ,$(LIBAXIDMA_INC_DIRS))

# The shared library files generated by compilation
LIBAXIDMA_NAME = axidma
LIBAXIDMA_LIBRARY = $(LIBAXIDMA_DIR)/lib$(LIBAXIDMA_NAME).so
LIBAXIDMA_OUTPUT_LIBRARY = $(OUTPUT_DIR)/lib$(LIBAXIDMA_NAME).so

# The Doxygen configuration file and generated libaxidma documentation file
LIBAXIDMA_DOC_CONFIG = libaxidma.dox
LIBAXIDMA_DOC = $(DOC_DIR)/html/index.html

################################################################################
# Targets
################################################################################

.PHONY: library library library_docs library_clean

# User-facing targets for compiling the library
library: $(LIBAXIDMA_OUTPUT_LIBRARY)

# Compile the library into a shared library file
$(LIBAXIDMA_LIBRARY): $(LIBAXIDMA) $(LIBAXIDMA_INC) | cross_compiler_check
	$(CC) $(LIBAXIDMA_CFLAGS) $(LIBAXIDMA_INC_FLAGS) $(filter %.c,$^) -o $@

# Copy the compiled shared library object to the specified output directory
$(LIBAXIDMA_OUTPUT_LIBRARY): $(LIBAXIDMA_LIBRARY) $(OUTPUT_DIR)
	@cp $< $@

# Geneate the Doxygen documentation for the library
library_docs:
	doxygen $(LIBAXIDMA_DOC_CONFIG) &> /dev/null
	firefox $(LIBAXIDMA_DOC) &

# Clean up all the files generated by compiling the library
library_clean:
	rm -f $(LIBAXIDMA_OUTPUT_LIBRARY) $(LIBAXIDMA_LIBRARY)

endif # LIBAXIDMA_MAKEFILE_
